- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortingAlgorithms.cpp
executable file
·205 lines (156 loc) · 4.53 KB
/
SortingAlgorithms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include"SortingAlgorithms.h"
#include<cmath>
SortingAlgorithms::SortingAlgorithms()
{
}
voidSortingAlgorithms::selectionSort(std::vector<int> &vElements)
{
// Initialization of variables
size_t smaller = 0;
int auxVector = 0;
// Goes through vector's elements
for(size_t i = 0; i < ( vElements.size() - 1); i++)
{
// Searching for the smallest element
smaller = i;
for(size_t j = i + 1; j < vElements.size(); j++)
{
if(vElements[j] < vElements[smaller])
smaller = j;
}
// If there was an swap, update vector's elements
if(smaller != i)
{
auxVector = vElements[smaller];
vElements[smaller] = vElements[i];
vElements[i] = auxVector;
}
}
}
voidSortingAlgorithms::insertSort(std::vector<int> &vElements)
{
/* DEBUG */
/* for(int i = 0; i < vElements.size(); i++)
{
std::cout<<i<<"\t"<<vElements[i]<<std::endl;
}*/
// Initialization of variables
int auxVector = 0;
int j = 0;
//Goes through vector's elements
for(size_t i = 1; i < vElements.size(); i++)
{
auxVector = vElements[i];
// Sending the highest values forward
for(j = i; ( (j > 0) && (auxVector < vElements[j - 1]) ); j--)
{
vElements[j] = vElements[j - 1];
}
vElements[j] = auxVector;
}
}
voidmerge(int begin, int center, int end, std::vector<int> &vElements)
{
// Initialization of variables
int iB = begin,
iC = center,
i = 0;
std::vector<int> vElementsCopy; //Auxiliary vector
vElementsCopy.reserve(vElements.size());
//Comparing the values of the center to the end, and sending the values to the vElementsCopy
while(iB < center && iC < end)
{
if(vElements[iB] <= vElements[iC])
vElementsCopy[i++] = vElements[iB++];
else
vElementsCopy[i++] = vElements[iC++];
}
//Sending the remaining values
while(iB < center)
vElementsCopy[i++] = vElements[iB++];
//Sending the remaining values
while(iC < end)
vElementsCopy[i++] = vElements[iC++];
//Sends the sorted values from vElementsCopy to vEtements
for( iB = begin; iB < end; iB++)
{
vElements[iB] = vElementsCopy[iB - begin];
}
vElementsCopy.clear();
}
voidSortingAlgorithms::mergeSort(int begin, int end, std::vector<int> &vElements)
{
if( begin < end - 1)
{
int center = std::floor( (begin + end) / 2 ); //Discovering the medium
mergeSort(begin, center, vElements); //From the beginning to the center
mergeSort(center, end, vElements); //From the center to the end
merge(begin, center, end, vElements); //From the beginning to the end
}
}
intpartition(int begin, int end, std::vector<int> &vElements)
{
// Initialization of variables
int left = begin, right = end;
int auxVector = 0, // swap of position
pivot = vElements[begin]; // Amount of swap
// Goes through vector's elements
while( right > left )
{
while(pivot >= vElements[left]) // Walking on the left knot
left++;
while(vElements[right] > pivot) // Coming back on the left right
right--;
if(right > left )
{
auxVector = vElements[left];
vElements[left] = vElements[right];
vElements[right] = auxVector;
}
}
vElements[begin] = vElements[right];
vElements[right] = pivot;
return right;
}
voidSortingAlgorithms::quickSort(int begin, int end, std::vector<int> &vElements)
{
if( end > begin)
{
int pivot = partition(begin, end, vElements); // Find out pivot
quickSort(begin, pivot - 1, vElements); //From the beginning to the pivot-1
quickSort(pivot + 1, end, vElements); //From the pivot to the end
}
}
voidheapify(int i, int n, std::vector<int> &vElements)
{
// Initialization of variables
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // Left
int r = 2 * i + 2; // Right
// If left child is larger than root
if (l < n && vElements[l] > vElements[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && vElements[r] > vElements[largest])
largest = r;
// If largest is not root
if (largest != i)
{
std::swap(vElements[i], vElements[largest]);
// call the sub-tree
heapify(largest, n, vElements);
}
}
voidSortingAlgorithms::heapSort(std::vector<int> &vElements)
{
// Build heap
for (int i = (vElements.size() / 2 - 1); i >= 0; i--)
heapify( i, vElements.size(), vElements);
for (int i = (vElements.size() - 1); i >= 0; i--)
{
// Moving the root to the end
std::swap(vElements[0], vElements[i]);
// call max heapify on the reduced heap
heapify(0, i, vElements);
}
}